package com.sxau.software_educational_system_backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.sxau.software_educational_system_backend.common.ErrorCode;
import com.sxau.software_educational_system_backend.constant.MessageConstant;
import com.sxau.software_educational_system_backend.constant.PasswordConstant;
import com.sxau.software_educational_system_backend.constant.StatusConstant;
import com.sxau.software_educational_system_backend.domain.entity.Admin;
import com.sxau.software_educational_system_backend.domain.req.ReqAdminDTO;
import com.sxau.software_educational_system_backend.domain.req.ReqLoginDTO;
import com.sxau.software_educational_system_backend.exception.BusinessException;
import com.sxau.software_educational_system_backend.exception.PasswordErrorException;
import com.sxau.software_educational_system_backend.service.AdminService;
import com.sxau.software_educational_system_backend.mapper.AdminMapper;
import com.sxau.software_educational_system_backend.utils.PasswordUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* @author mi
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2025-05-07 20:35:58
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Override
    public Admin login(ReqLoginDTO reqLoginDTO) throws AccountNotFoundException, AccountLockedException {
        //1、根据用户名查询数据库中的数据
        Admin admin = lambdaQuery().eq(Admin::getUsername, reqLoginDTO.getUsername()).one();

        //2、处理各种异常情况（用户名不存在、密码不对、账号被注销）
        if (admin == null) {
            //账号不存在
            throw new BusinessException(ErrorCode.NOT_EXIT);
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        String inputPassword = reqLoginDTO.getPassword();
        String storedHash = admin.getPassword();
        //验证密码
        Boolean isTure=PasswordUtils.checkPassword(inputPassword, storedHash);
        if (!isTure) {
            //密码错误
            System.out.println("密码错误");
            throw new BusinessException(ErrorCode.PASSWORD_ERROR);
        }

        if (StatusConstant.DISABLE.equals(admin.getIsDelete())) {
            //账号被锁定
            throw new BusinessException(ErrorCode.ACCOUNT_CANCEL);
        }

        //3、返回实体对象
        return admin;
    }

    @Override
    public int saveUser(ReqAdminDTO reqAdminDTO) {
        Admin admin = new Admin();
        //对新增管理员密码进行加密
        String encryptedPwd = PasswordUtils.hashPassword(reqAdminDTO.getPassword());
        System.out.println(encryptedPwd);
        reqAdminDTO.setPassword(encryptedPwd);


        //对象属性拷贝
        BeanUtils.copyProperties(reqAdminDTO, admin);
        //设置账号的状态，默认正常状态 0表示启用 1表示禁用
        admin.setIsDelete(StatusConstant.ENABLE);
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        this.save(admin);

        return admin.getId();
    }
}




